
// 对应编码范围创建字符放到数组中
var numList = [];
for (var i = 48; i <= 57; i++) {
    var char = String.fromCharCode(i);
    numList.push(char);
}
console.log(numList);

var bigList = [];
for (var i = 65; i <= 90; i++) {
    var char = String.fromCharCode(i);
    bigList.push(char);
}
console.log(bigList);

var smallList = [];
for (var i = 97; i <= 122; i++) {
    var char = String.fromCharCode(i);
    smallList.push(char);
}
console.log(smallList);

var speList = ["_"]; //允许使用的特殊字符
var normalList = numList.concat(bigList, smallList, speList);
console.log(normalList);


/* function randCode() {
    var list = numList.concat(bigList, smallList);

    var str = "";
    for (var i = 0; i < 4; i++) {
        var index = Math.floor(Math.random() * list.length);
        var char = list[index];
        str += char;
    }
    return str;
}*/

// 不重复
/* function randCode() {
    var list = numList.concat(bigList, smallList);

    var str = "";
    for (var i = 0; i < 4; i++) {
        var index = Math.floor(Math.random() * list.length);
        var char = list[index];
        if(!str.includes(char)){
            str += char;
        }else{
            i--;
        }
    }
    return str;
} */


// 数字 大写 小写 至少有一个

function randCode() {
    var list = numList.concat(bigList, smallList);

    var str = "";
    for (var i = 0; i < 4; i++) {
        var index = Math.floor(Math.random() * list.length);
        var char = list[index];
        if(!str.includes(char)){
            str += char;
        }else{
            i--;
        }
    }

    var isExistNum = false;
    var isExistBig = false;
    var isExistSmall = false;

    for (var val of str) {
        if (numList.includes(val)) isExistNum = true;
        else if (bigList.includes(val)) isExistBig = true;
        else if (smallList.includes(val)) isExistSmall = true;
    }

    var level = isExistNum + isExistBig + isExistSmall ;
    
    if(level == 3) return str;
    // console.log("再来一次");
    return randCode();
}
